//
//  ChangepwdViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/2.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ChangepwdViewController.h"

#import "HeaderFileForEasyToTesco.h"
#import "AFNetworking.h"

#import "RKAlertView.h"
#import "ReadLoginData.h"


@interface ChangepwdViewController ()<UITextFieldDelegate>

@end

@implementation ChangepwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    
    [self changepwdtopview];
    
    [self getdata];
    
}

- (void)getdata {
<<<<<<< HEAD
    // 获取当前账号
=======
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请登录后再次操作" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
    }else {
        self.chpwdUsername.text = [dic[@"data"] objectForKey:@"username"];
    }
}

- (void)changepwdtopview {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"修改登陆密码";

}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (IBAction)changeOverBtnClick:(id)sender {
    
    if (self.chpwdUsername.text.length != 0 && self.OldPassword.text.length != 0 && self.NewPassword.text.length != 0 && self.NewPassWordTwo.text.length != 0) {
        
        if ([self.NewPassword.text isEqualToString:self.NewPassWordTwo.text]) {
            [self ChangePwddatasources];
        } else {
            UIAlertView *alview =[[ UIAlertView alloc] initWithTitle:@"提示" message:@"您两次输入的新密码不一致,请重新输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alview show];
            
        }
        
    } else {
        UIAlertView *alview =[[ UIAlertView alloc] initWithTitle:@"提示" message:@"请完善您的信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
    }
    
    

}

- (void)ChangePwddatasources {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = self.chpwdUsername.text;
    NSString *password = self.NewPassword.text;
    NSString *oldpwd = self.OldPassword.text;

    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", username], @"password":[NSString stringWithFormat:@"%@", password], @"oldpwd":[NSString stringWithFormat:@"%@", oldpwd]};
    
    __weak ChangepwdViewController *changeself = self;
    
    [mgr POST:@"http://www.ylgw8.com/AppUser/Editpwd" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *dict = [changeself toArrayOrNSDictionary:responseObject];
        
        UIAlertView *alview =[[ UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", dict[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
        
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
//         NSLog(@"数据请求错误原因:%@", error);
//         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}


// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}

//触摸空白处回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    
    //    [self.usernameText resignFirstResponder];
    [self.OldPassword resignFirstResponder];
    [self.NewPassword resignFirstResponder];
    [self.NewPassWordTwo resignFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
